package com.youth.framework.util;

public final class CryptorFactory {
	public static final String CRYPTOR_CLASS = "org.weibo.common.cryptor";
	private static Cryptor instance;

	public static synchronized void setCryptor(Cryptor cryptor) {
		instance = cryptor;
	}

	public static synchronized Cryptor getCryptor() {
		if (instance != null) {
			return instance;
		}

		String cryptorClass = System
				.getProperty(CRYPTOR_CLASS);
		if (cryptorClass != null) {
			try {
				Class clazz = Class.forName(cryptorClass);
				instance = (Cryptor) clazz.newInstance();
				return instance;
			} catch (Exception ex) {
				throw new RuntimeException("Unable to create Cryptor "
						+ cryptorClass, ex);
			}
		}
		instance = new DefaultCryptor();
		return instance;
	}

	public static void main(String[] args) throws Exception {
		String str = "adasai";
		Cryptor cryptor = CryptorFactory.getCryptor();
		String encryptStr = cryptor.encrypt(str);
		System.out.println(encryptStr);
		System.out.println(cryptor.decrypt(encryptStr));
	}
}
